/**
 * Input is ratings / residuals data, sorted by movies, of the form:
 * movie_id:
 * user_id,rating[,...]
 * ...
 * where movie_ids must appear in ascending sorted order and, within each
 * movie block, user_ids must appear in ascending sorted order. (Here I mean
 * numeric order, not alphanumeric order.)
 *
 * It outputs a NUM_MOVIES by NUM_MOVIES symmetric matrix where entry (i, j)
 * is the number of users that rated both movie i+1 and movie j+1, in binary
 * format (floats) on stdout. (The '+1's above are because i and j are
 * zero-based, but the movie ids are one-based).
 */
#include <cassert>
#include <iostream>
#include <vector>
#include "u1.h"
#include "coratings.h"
#include "symmetric_matrix.h"

using namespace std;

// ~100M ratings + ~18k separators for movies.
#define MAX_RATINGS 101000000
id_rating_t<float> r[MAX_RATINGS];

// Functor for use with for_coratings that computes pearson correlation 
// for coratings of each pair of movies.
symmetric_matrix<float> U(NUM_MOVIES);
struct F_comp
{
  int pi, pj, count;

  void begin_pair(int id_i, int id_j) {
    pi = id_i - 1;
    pj = id_j - 1;
    count = 0;
  }

  void corating(int id, float rating_i, float rating_j) {
    ++count;
  }

  void end_pair() {
    assert(count >= 0);
    U(pi, pj) = (float)count;
  }
} comp;

int main(int argc, char **argv)
{
  TRACE<<"reading coratings"<<endl;
  size_t n = read_ratings_blocks(r, MAX_RATINGS, cin);
  assert(n < MAX_RATINGS);

  TRACE<<"computing"<<endl;
  for_coratings(r, n, comp);

  TRACE<<"done computing; writing output"<<endl;
  U.write_binary(cout);

  TRACE<<"done"<<endl;
  return 0;
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

